package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.service.DepartmentService;
import com.hdax.service.EmployeeInfoService;
import com.hdax.service.EmployeeService;
import com.hdax.service.PositiveService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 转正申请控制器
 *
 * @author Aizen
 * @date 2022/06/04
 */
@Controller
@RequestMapping("/positive")
public class PositiveController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 转正服务
     */
    @Autowired
    private PositiveService positiveService;

    /**
     * 部门服务
     */
    @Autowired
    private DepartmentService departmentService;

    /**
     * 员工信息服务
     */
    @Autowired
    private EmployeeInfoService employeeInfoService;

    @GetMapping(path = "/index")
    @Transactional(rollbackFor=Exception.class)
    public String list(Model model, HttpSession session) {
        //添加满3个月的人
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String wagesDate=fmt.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -3);
        //获取三个月前的日期
        String posidate = fmt.format(calendar.getTime());
        //查询所有的员工
        List<EmployeeInfo> emplist=employeeInfoService.findAllnotZhengShi();
        for (int i = 0; i < emplist.size(); i++) {
            //查询员工的入职日期
            String shijain=emplist.get(i).getEmpRuZhiDate();
            //
            if (shijain==null || shijain==""){
                continue;
            }else{
                int comTo=shijain.compareTo(posidate);
                if (comTo==0){
                    Positive p = new Positive();
                    p.setFiledDate(wagesDate);
                    p.setEmpId(emplist.get(i).getId());
                    p.setFiledText("试用期三个月已到");
                    positiveService.insertByPo(p);
                }else if (comTo<-3){
                    //判断转正申请表里是否有该员工
                    Integer empCount = positiveService.findByid2(emplist.get(i).getId());
                    if (empCount==0){
                        Positive p=new Positive();
                        p.setFiledDate(wagesDate);
                        p.setEmpId(emplist.get(i).getId());
                        p.setFiledText("试用期三个月已到");
                        positiveService.insertByPo(p);
                    }
                }
            }
        }

        //获取当前的编号,判断是管理员还是项目经理
        Employee employeeInfo=(Employee)session.getAttribute("loginUser");
        int id=employeeInfo.getEmpId();
        List<Department> departmentList=departmentService.listAll(id);
        List<EmployeeInfo> positives=new ArrayList<>();
        //查询是否是管理员
        if (departmentList.size()>0){
            for (int i = 0; i < departmentList.size(); i++) {
                int did=departmentList.get(i).getId();
                if (did==1){
                    //管理员
                    model.addAttribute("guanli",did);
                    //查询所有未转正的员工
                    List<EmployeeInfo> employeeInfoList=employeeInfoService.selectAll();
                    for (int j = 0; j < employeeInfoList.size(); j++) {
                        if (employeeInfoList.get(j).getEmpZhuanZhengDate()==null){
                            positives.add(employeeInfoList.get(j));
                        }
                    }
                    model.addAttribute("yuangong",positives);
                    break;
                }else{
                    //当前项目经理
                    int dpid=departmentList.get(i).getDperson();
                    if (dpid==id){
                        //根据部门编号查询员工
                        List<EmployeeInfo> employeeInfoList=employeeInfoService.emplist(did);
                        for (int h = 0; h < employeeInfoList.size(); h++) {
                            if (employeeInfoList.get(h).getId()==1){
                                continue;
                            }else{
                                //获取员工姓名并判断是否是非正式员工
                                String state=employeeInfoList.get(h).getEmpZhuanZhengDate();
                                if (state==null){
                                    positives.add(employeeInfoList.get(h));
                                }
                            }
                        }
                    }
                    model.addAttribute("yuangong",positives);
                    model.addAttribute("jingli",did);
                }
            }
        }else{
            //普通员工
            //根据id查询员工名字
            positives.add(employeeInfoService.findEmployeeInfoById(id));
            model.addAttribute("yuangong",positives);
            model.addAttribute("putong",departmentList.size());
        }
        return "positive/PositiveProfilres";
    }

    @PostMapping(path = "/list")
    @ResponseBody
    public Map<String,Object> list(int page,int limit, Positive positive, int id){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        int count=0;
        if (id!=1){
            positive.setEmpId(id);
        }
        if(positive.getEmpId()!=null){
            List<Positive> p =positiveService.findAll((page-1)*limit,limit,positive);
            map.put("count", p.size());
            map.put("data", p);
        }else{
            List<Department> departmentList=departmentService.listAll(id);
            if (page-1!=0 || limit!=0){
                List<Positive> p =positiveService.findAll((page-1)*limit,limit,positive);
                map.put("count",positiveService.count(positive));
                map.put("data", p);
            }else if (departmentList.size()>0){
                for (int i = 0; i < departmentList.size(); i++) {
                    int did=departmentList.get(i).getId();
                    if (did==1){
                        //管理员
                        positive.setEmpId(null);
                        List<Positive> p =positiveService.findAll((page-1)*limit,limit,positive);
                        map.put("count", p.size());
                        map.put("data", p);
                        break;
                    }else{
                        //当前项目经理
                        List<Positive> p=new ArrayList<>();
                        int dpid=departmentList.get(i).getDperson();
                        if (dpid==id){
                            //根据部门编号查询员工
                            List<EmployeeInfo> employeeInfoList=employeeInfoService.emplist(did);
                            for (int h = 0; h < employeeInfoList.size(); h++) {
                                int eid=employeeInfoList.get(h).getId();
                                positive.setEmpId(eid);
                                List<Positive> pi =positiveService.findAll((page-1)*limit,limit,positive);
                                for (int j = 0; j < pi.size(); j++) {
                                    int pid=pi.get(j).getId();
                                    Positive pos=positiveService.findByid(pid);
                                    p.add(pos);
                                    count++;
                                }
                            }
                        }
                        map.put("count", count);
                        map.put("data", p);
                    }
                }
            }else{
                //单个员工
                positive.setEmpId(id);
                List<Positive> p =positiveService.findAll(page-1,limit,positive);
                //查询总条数(该名称下)
                map.put("count", p.size());
                map.put("data", p);
            }
        }
        return map;
    }

    @GetMapping(path = "/insert")
    public String insert(Model model,int id) {
        //获取要展示的员工列表
        List<EmployeeInfo> positives=new ArrayList<>();
        if (id==1){
            List<EmployeeInfo> employeeInfoList=employeeInfoService.selectAll();
            for (int j = 0; j < employeeInfoList.size(); j++) {
                if (employeeInfoList.get(j).getEmpZhuanZhengDate()==null){
                    positives.add(employeeInfoList.get(j));
                }
            }
        }else{
            //根据id查询是否是项目经理
            List<Department> departmentList=departmentService.listAll(id);
            if (departmentList.size()>0){
                for (int i = 0; i < departmentList.size(); i++) {
                    //获取部门id,查询对应的角色
                    int did=departmentList.get(i).getId();
                    List<EmployeeInfo> employeeInfoList= employeeInfoService.emplist(did);
                    //循环判断是否转正
                    for (int j = 0; j < employeeInfoList.size(); j++) {
                        if (employeeInfoList.get(j).getEmpZhuanZhengDate()==null){
                            positives.add(employeeInfoList.get(j));
                        }
                    }
                }
            }
        }
        model.addAttribute("pname",positives);
        return "positive/insert";
    }

    /**
     * 添加
     * @param
     * @return
     */
    @PostMapping(path = "/insertposi")
    @ResponseBody
    public Map<String,Object> insertposi(Positive positive){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        int resule=positiveService.insertByPo(positive);
        if (resule>0){
            map.put("msg","添加成功");
            map.put("data","success");
        }else {
            map.put("msg","添加失败");
            map.put("data","fail");
        }
        return map;
    }

    /**
     * 审批
     */
    @GetMapping(path = "/edit")
    public ModelAndView edit(Integer pid) {
        //根据ID查到员工信息
        Positive positive = positiveService.findByid(pid);
        ModelAndView mav = new ModelAndView("positive/edit");
        mav.addObject("positive",positive);
        return mav;
    }


    @ResponseBody
    @PostMapping(path = "updateposi")
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> updateposi(Positive positive){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        //获取当前时间
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        String wagesDate=formatter.format(date);
        positive.setVettingDate(wagesDate);
        positive.setVettingName("admin");
        positive.setState(positive.getVettingOut());
        int resule= positiveService.updatePosi(positive);
        if (resule>0){
            if (positive.getVettingOut()==13){
                EmployeeInfo ef=new EmployeeInfo();
                ef.setEmpZhuanZhengDate(wagesDate);//转正日期
                ef.setId(positive.getEmpId());
                employeeInfoService.updateById(ef);
                Employee employee = new Employee();
                employee.setEmpId(ef.getId());
                employee.setIsZhengShi(1);
                employeeService.updateById(employee);
            }
            map.put("msg","审批成功");
            map.put("data","success");
        }else {
            map.put("msg","审批失败");
            map.put("data","fail");
        }
        return map;
    }
}
